<?php
/**
*	users模块接口文件
*/
namespace Rainrock\Framework\Module;


use Rainrock\Framework\kernel\core\ModuleListener;
use Rainrock\Framework\kernel\base\Auth;
use Rainrock\Framework\kernel\base\Request;
use Rainrock\Framework\kernel\base\Jiami;
use Rainrock\Framework\kernel\Html\HtmlFun;
use Rainrock\Framework\kernel\Html\HtmlButton;

class UsersModuleListener extends ModuleListener{
	
	
	public function onListRightbtn($pnum)
	{
		$btn[] = HtmlButton::gets()->addCont('更新微信状态')->setClickevt('wxqystate')->addCss('webbtn-default');
		$btn[] = HtmlButton::gets()->addCont('发微信消息')->setClickevt('wxqysend')->addCss('webbtn-default');
		return $btn;
	}
	
	public function onListScript()
	{
		$barr['wxqystate']		= HtmlFun::get()
			->addCode('if(!this.table.changeid){js.msg(\'msg\',\'未选中\');return;}')	
			->addCode('this.query(\'wxqystate\',{id:this.table.changeid})');

		$barr['wxqysend']		= HtmlFun::get()
			->addCode('if(!this.table.changeid){js.msg(\'msg\',\'未选中\');return;}')	
			->addCode('var me = this;')	
			->addCode('js.prompt(\'发测试微信消息\',function(jg,txt){if(jg==\'yes\' && txt)me.wxqysends(txt)}, \'请输入内容\',\'\')');
			
		$barr['wxqysends']		= HtmlFun::get()->addCan('txt')->addCode('this.query(\'wxqysend\',{id:this.table.changeid,cont:jm.base64encode(txt)})');
		return $barr;
	}
	
	public function onSaveBefore($arr, $id, $isadd)
	{
		$pass 	= $arr['pass'];
		$rand 	= '';
		if($isadd && $this->isempt($pass)){
			$rand = time();
			$pass = '123456';
		}
		if(!$isadd && !$this->isempt($pass)){
			$rand = time();
		}
		$data 	= array();
		$nosave = array();
		if($rand){
			$data['rand'] = $rand;
			$data['pass'] = md5($pass.$rand);
		}else{
			$nosave[] = 'pass';
			$nosave[] = 'rand';
		}
		return array(
			'data' 	 => $data,
			'nosave' => $nosave,
		);
	}
	
	public function onDeleteBefore()
	{
		if($this->mid==Auth::$USERID)return '当前登录用户不能删除';
		if(Auth::$TYPE == 2)return '超级管理员不能删除';
	}
	
	public function onReplacers($rs, $lx)
	{
		$rs['pass_textcn'] = '不显示';
		$rs['pass_color'] = 'gray';
		return $rs;
	}
	
	/**
	*	微信状态
	*/
	public function wxqystate()
	{
		$id 	= Request::getint('id');
		$urs 	= $this->model->getone($id);
		$mobile = $urs['mobile'];
		if(!$mobile)return $this->returnerror('未设置手机号');
		$mobile	= Jiami::base64decode($mobile);
		
		$barr 	= $this->c('wxqy')->usergetuserid($mobile);
		if(!$barr['success']){
			$this->model->update(array('wxstate' => 0,'wxuser'  => '',),$id);
			return $barr;
		}
		$data 	= $barr['data'];
		$this->model->update(array(
			'wxstate' => $data['wxstate'],
			'wxuser'  => $data['wxuser'],
		),$id);
		return $this->returnsuccess();
	}
	
	/**
	*	发测试消息
	*/
	public function wxqysend()
	{
		$id 	= Request::getint('id');
		$cont 	= Request::get('cont');
		$urs 	= $this->model->getone($id);
		$wxstate= $urs['wxstate'];
		$wxuser = $urs['wxuser'];
		if($wxstate != 1)return $this->returnerror('未激活无法发送');
		return $this->c('wxqy')->sendtext($wxuser, Jiami::base64decode($cont));
	}
}